package com.iwinding.ladygg;

import android.content.Context;
import android.text.TextUtils;

import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.controller.EaseUI;
import com.hyphenate.easeui.domain.EaseUser;
import com.iwinding.ladygg.modle.db.bean.Users;
import com.iwinding.ladygg.modle.db.impl.UserDBHelper;
import com.iwinding.ladygg.net.RequestManager;
import com.iwinding.ladygg.net.callback.ContactsCallback;
import com.iwinding.ladygg.net.response.LoginRp;
import com.iwinding.ladygg.net.response.UserRp;
import com.iwinding.ladygg.utils.SystemUtil;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

/**
 * Created by hezhiyong on 2017/4/21.
 */

public class SOSUserProfileProvider implements EaseUI.EaseUserProfileProvider {

    private Map<String, EaseUser> contactList;
    private Map<String, EaseUser> contactGroupList;
    private Context mContext;

    private static SOSUserProfileProvider INSTANCE = new SOSUserProfileProvider();

    public static SOSUserProfileProvider getInstance() {
        return INSTANCE;
    }

    public SOSUserProfileProvider() {
    }

    public EaseUser getUser(String username) {
        return getUserInfo(username);
    }

    private EaseUser getUserInfo(String username) {
//获取 EaseUser实例, 这里从内存中读取
        //如果你是从服务器中读读取到的，最好在本地进行缓存
        EaseUser user = null;
        //如果用户是本人，就设置自己的头像
        LoginRp loginRp = SystemUtil.getLoginRp(mContext);
        if (username.equals(EMClient.getInstance().getCurrentUser())) {
            user = new EaseUser(username);
            user.setAvatar(loginRp.headImg);
            user.setNick(loginRp.getName());
            return user;
        }
        //如果用户是本人groupId，就设置自己的头像
        if (username.equals(loginRp.groupId)) {
            user = new EaseUser(loginRp.groupId);
            user.setAvatar(loginRp.headImg);
            user.setNick(loginRp.getName());
            return user;
        }

//        if (user==null && getRobotList()!=null){
//            user=getRobotList().get(username);
//        }
        //收到别人的消息，设置别人的头像
        if (contactList != null && contactList.containsKey(username)) {
            user = contactList.get(username);
        }
        //收到别人的消息，设置别人的头像
        if (contactGroupList != null && contactGroupList.containsKey(username)) {
            user = contactGroupList.get(username);
        }
        //如果用户不是你的联系人，则进行初始化
        if (user == null) {
            user = new EaseUser(username);
        } else {
            if (TextUtils.isEmpty(user.getNick())) {//如果名字为空，则显示环信号码
                user.setNick(user.getUsername());
            }
        }
        return user;
    }

    public void initContext(Context mContext) {
        this.mContext = mContext;
    }

    public void initContactList() {
        List<Users> userses = UserDBHelper.getInstance(mContext).queryUsers();
        contactList = new Hashtable<>();
        contactGroupList = new Hashtable<>();
        if (userses != null) {
            for (Users userse : userses) {
                EaseUser user = EaseUser.initUser(userse);
                contactList.put(user.getUsername(), user);
                if (!TextUtils.isEmpty(userse.getGroupId()))
                    contactGroupList.put(userse.getGroupId(), user);
            }
        }
        RequestManager.postContacts(mContext, new ContactsCallback() {
            @Override
            public void onResult(List<UserRp> userRps) {
                List<Users> userses = new ArrayList<Users>();
                contactList.clear();
                contactGroupList.clear();
                if (userRps != null) {
                    for (UserRp userRp : userRps) {
                        Users toUsers = userRp.toUsers();
                        userses.add(toUsers);

                        EaseUser user = EaseUser.initUser(toUsers);

                        contactList.put(user.getUsername(), user);
                        if (!TextUtils.isEmpty(toUsers.getGroupId()))
                            contactGroupList.put(toUsers.getGroupId(), user);
                    }
                }
                UserDBHelper.getInstance(mContext).addAllUsers(userses);
            }

            @Override
            public void onFail(String errorMsg) {

            }
        });
    }

    public EaseUser queryUserByGroupId(Context mContext, String groupId) {
        Users users = UserDBHelper.getInstance(mContext).queryUserByGroupId(groupId);
        if (users == null)
            return null;
        return EaseUser.initUser(users);
    }

}
